function EventAlbumController() {
	
	jQuery('.albumEditableContainer').hide();
	var unselectedPhotosContainer = jQuery('#unselectedPhotosGridContainer');	
	var selectedPhotosContainer = jQuery('#gallery');
	var imagePreviewContainer = jQuery('#preview-image');
	var coverPhotoImage = jQuery('#coverPhotoImage');
	var hiddenCoverId = jQuery('#hidden_cover_id');
	var hiddenSelectedPhotos = jQuery('#hidden_selected_photos_ids');
	
	var UNSELECTED_PREFIX = "Unselected";
	var SELECTED_PREFIX = "Selected";
	
	var albumEntity = new Album();
	
	
	this.editAlbum = editAlbum;
	this.loadSelectedPhotos = loadSelectedPhotos;
	
	this.addPhotoToUnselectedPhotos = addPhotoToUnselectedPhotos;
	this.setHiddenFields = setHiddenFields;
	this.setEditMode = setEditMode;
	
	function editAlbum(postingUrl,albumId,eventId) {		
		jQuery.ajax({
            url: postingUrl,//	,
            datatype:"json",
            type: "POST",
            //data: { "albumId": albumId, "eventId": eventId},            
            data: { "albumId": albumId, "eventId": 1},
            async:true,		                
            success: function(data){		                    	            	
            	setEditMode(data);
            },
            error: function(){		                		                				       
            	alert("Failure");		                   
            }	                
        });				
	}
	
	function setEditMode(unselectedPhotos){
		jQuery('.albumEditableContainer').show();
    	loadUnselectedPhotos(unselectedPhotos);            
		imagePreview();
		setEditVisibility();
	}
	
	function setEditVisibility(){
		jQuery('#btn_edit').hide();
		jQuery('.albumEditableContainer').show();
		jQuery('.editField').show();
		jQuery('.showField').hide();
	}
	
	function setShowVisibility(){
		jQuery('#btn_edit').show();
		jQuery('.albumEditableContainer').hide();
		jQuery('.editField').hide();
		jQuery('.showField').show();
	}
	
	function loadSelectedPhotos(photosJSONList){

		jQuery.each(photosJSONList, function()  {			
			var elementName = SELECTED_PREFIX+this.photoId;
			//var photoEntity = new Photo(this.photoId,"../"+this.url,elementName,"../"+this.thumbnailUrl);
			var photoEntity = new Photo(this.photoId,this.url,elementName,this.thumbnailUrl,this.thumbnailMediumUrl);
			addPhotoToAlbum(photoEntity);	
			setShowVisibility();
		});		
		
	}
	
	function loadUnselectedPhotos(photosJSONList){
		jQuery.each(photosJSONList, function()  {
			var photoJSON = this; 
			var elementName = UNSELECTED_PREFIX+photoJSON.photoId;
			//var photoEntity = new Photo(photoJSON.photoId,"../"+photoJSON.url,elementName,"../"+photoJSON.thumbnailUrl);
			var photoEntity = new Photo(photoJSON.photoId,photoJSON.url,elementName,photoJSON.thumbnailUrl,photoJSON.thumbnailMediumUrl);
			addPhotoToUnselectedPhotos(photoEntity);							
		});
		imagePreview();
	}
	
	
	
	function addPhotoToUnselectedPhotos(photoEntity){				
		albumEntity.addPhotoToUnselectedPhotos(photoEntity);
													
		var aPhotoContainer = jQuery('<div id="'+photoEntity.getElementName()+'" class="gridPhotoContainer" />'); 			  
		var galleryRef = jQuery('<a class="preview" />');		
									
		var image = jQuery('<img class="gridPhotoImage" >');		
		image.prop('src',photoEntity.getThumbnailUrl());			
		
		image.click(function (event) {		
			//Hace que no se redireccione a la imagen.
			event.preventDefault();
			
			var selectedElement = jQuery(this).parent().parent();
			var selectedElementName = selectedElement.prop('id');			
			var selectedPhoto = albumEntity.getPhotoEntityByElementName(selectedElementName);
			selectedPhoto.setElementName(SELECTED_PREFIX+selectedPhoto.getId());			
			selectedElement.remove();
			jQuery('#preview-image').remove();
			
			
			addPhotoToAlbum(selectedPhoto);			
	    });
				
		galleryRef.append(image);				
		galleryRef.prop('href', photoEntity.getUrl())						  				

		aPhotoContainer.append(galleryRef);
		aPhotoContainer.appendTo(unselectedPhotosContainer);			
	}
	
	
	
	
	/* La estructura que arma es de la siguiente forma 
	<div id={elementName} class="gridMediumImageContainer span3" >																																			
		<a rel="gallery" href={fotoEntera}>
			<img class="gridMediumImage" src={fotoTamañoMedio} />								
		</a>
		<div class="gridMediumImageBottom albumEditableContainer" >									
		 	<div class="gridMediumImageBottomButton deleteImageBackground" />
		 	<div class="gridMediumImageBottomButton coverImageBackground" />
		</div> 		
	</div>			
	 */
	function addPhotoToAlbum(photoEntity){			
		//Agrego la entidad a la lista de fotos.
		albumEntity.addPhotoToAlbum(photoEntity);		
		
		var aPhotoContainer = jQuery('<div id="'+photoEntity.getElementName()+'" class="gridMediumImageContainer span3" />');
		var galleryRef = jQuery('<a rel="gallery" href="'+photoEntity.getUrl()+'" />');
		var image = jQuery('<img class="gridMediumImage" src="'+photoEntity.getThumbnailMediumUrl()+'" />');
		//Agrego la imagen a la gallery.
		galleryRef.append(image);
		
		var aPhotoContainerFooter = jQuery('<div class="albumEditableContainer gridMediumImageBottom" />');
		//Agrego los botones al footer
		var deleteImageButton = jQuery('<div class="gridMediumImageBottomButton deleteImageBackground" />');
		var coverImageButton = jQuery('<div class="gridMediumImageBottomButton coverImageBackground" />');
		aPhotoContainerFooter.append(deleteImageButton);
		aPhotoContainerFooter.append(coverImageButton);
		
		//Cuando intenta eliminar, vuelve al listado de fotos disponibles. 
		deleteImageButton.click(function (event) {		
			
			var selectedElement = jQuery(this).parent().parent();
			var selectedElementName = selectedElement.prop('id');			
			var selectedPhoto = albumEntity.getPhotoEntityByElementName(selectedElementName);
			
			//Si borro la foto que es el cover, se queda en nulo el cover.
			if( selectedPhoto.getId() == coverPhotoImage.attr('name')){
				setNullCover();
			};
			
			selectedPhoto.setElementName(UNSELECTED_PREFIX+selectedPhoto.getId());			
			selectedElement.remove();															
			
			addPhotoToUnselectedPhotos(selectedPhoto);	
			imagePreview();
	    });
		
		coverImageButton.click(function (event) {					
			var selectedElement = jQuery(this).parent().parent();
			var selectedElementName = selectedElement.prop('id');			
			var selectedPhoto = albumEntity.getPhotoEntityByElementName(selectedElementName);			
			// cambio la imagen de la cover foto
			coverPhotoImage.attr('src',selectedPhoto.getUrl());
			//coverPhotoImage.attr('src',"/PhotoAdminGrailsApp/images/upload/1/IMG00361-20120825-0156.jpg");
			coverPhotoImage.attr('name',selectedPhoto.getId());
	    });
		
		
		aPhotoContainer.append(galleryRef);
		aPhotoContainer.append(aPhotoContainerFooter);
					
		aPhotoContainer.appendTo(selectedPhotosContainer);		
	}
	
	function setNullCover(){
		//coverPhotoImage.attr('src',"../../images/defaultImage.jpg");
		coverPhotoImage.attr('src',"/PhotoAdminGrailsApp/images/defaultImage.jpg");
		coverPhotoImage.attr('name',"0");
	}
	
	function setHiddenFields(){		
		hiddenSelectedPhotos.val(albumEntity.getSelectedPhotosIds());							
		hiddenCoverId.val(coverPhotoImage.attr('name'));
	}	
	
}